<template>
    <div>
            <el-form
            :inline="true"
            label-position="right"
            ref="form"
            :model="form"
            label-width="120px"
            :rules="rules"
            >
            <span v-for="item in formcontent" :key="item.prop">
                <el-form-item :label="item.label" :prop="item.prop">
                    <el-input v-model='form[item.prop]' :placeholder="item.placeholder"></el-input>
                </el-form-item>
            </span>
            <el-form-item label="地址" prop="province">
                <AddressCascader @getaddress="getaddress"></AddressCascader>
            </el-form-item>
            <el-form-item style="margin-left:700px">
                <el-button type="primary" @click="submit()">确定</el-button>
                <el-button @click="clearform()">取消</el-button>
            </el-form-item>
            </el-form>
    </div>
</template>

<script>
import AddressCascader from '../../common/AddressCascader'
export default {
    name: 'Dialogform',
    props: {
        // 接收一个对象 表单内容
        formcontent: {
            type: Array,
            required: true
        },
        // from初始化对象
        form: {
            type: Object,
            required: true
        },
        // 关闭时是否置空
        reset: {
            type: Boolean,
            default: false
        }   
    },
    components: {
        AddressCascader
    },
    data() {
        return{
            // 表单验证规则
            rules: {
                name: [{required: true,message:'请输入基地名称'}],
                principalName: [{required:true,message:'请输入负责人姓名'}],
                registerNumber: [{required:true,message: '请输入注册号'}],
                province: [{required:true,message: '请输入地址'}],
                // city: [{required:true,message: '请输入注册基地所在城市'}],
                // region: [{required:true,message: '请输入注册基地所在区'}]
            }
        }
    },
    methods: {
        // 表单传回父组件
        submit() {
            this.$emit("submit",this.form)
        },
        // 表单置空方法
        clearform() {
            if(this.reset == true) {
                this.resetform()
            }
            this.$emit('closeform')
        },
        resetform() {
            this.formcontent.forEach(item => {
                this.form[item.prop] = ''
            })
        },
        getaddress(addres) {
            this.form.province = addres[0]
            this.form.city = addres[1]
            this.form.region = addres[2]
        }
    }
}
</script>

<style lang="sass" scoped>
.el-form .el-form-item .el-input {
  width: 300px;
}
</style>